package com.tiger.cloud.service.crm;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.tiger.cloud.common.exception.ServiceException;
import com.tiger.cloud.common.jpush.JpushInterface;
import com.tiger.cloud.common.mybatis.MybatisTigerDao;
import com.tiger.cloud.common.mybatis.TigerBaseService;
import com.tiger.cloud.common.token.PasswordHashUtil;
import com.tiger.cloud.dao.crm.PeersDao;
import com.tiger.cloud.dao.crm.UserAccountDao;
import com.tiger.cloud.dao.crm.UserDao;
import com.tiger.cloud.dao.crm.UserInfoDao;
import com.tiger.cloud.dao.share.ShareRecipientLogDao;
import com.tiger.cloud.enums.DeleteEnum;
import com.tiger.cloud.enums.PointCodeEnum;
import com.tiger.cloud.enums.UserResourceEnum;
import com.tiger.cloud.enums.UserStatusEnum;
import com.tiger.cloud.form.UserLoginForm;
import com.tiger.cloud.model.crm.User;
import com.tiger.cloud.model.crm.UserAccount;
import com.tiger.cloud.model.crm.UserInfo;
import com.tiger.cloud.model.share.PointCodeMessage;
import com.tiger.cloud.util.ErrorCode;
import com.tiger.cloud.util.ShareCodeUtil;

import cn.jpush.api.push.PushResult;

/**
 * 用户管理
 */
@Service("userService")
public class UserService extends TigerBaseService<User> implements IUserService {

	@Autowired
	private UserDao userDao;
	@Autowired
	private UserInfoDao userInfoDao;
	@Autowired
	private UserAccountDao userAccountDao;

	@Autowired
	private ShareRecipientLogDao shareRecipientLogDao;
	@Autowired
	private IUserConsumeService userConsunmeService;

	@Autowired
	private PeersDao peersDao;
	@Autowired
	private JpushInterface ipushInterface;

	
	@Override
	public MybatisTigerDao<User> getEntityDao() {
		return userDao;
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public boolean sendJpushMessage(List<String> jpushCodeList) {
		if(jpushCodeList!=null&&jpushCodeList.size()>0){
			List<String> aliasesList=new ArrayList<String>();
			for (String jpushCode : jpushCodeList) {
				if((!StringUtils.isEmpty(jpushCode)) &&jpushCode.length()>10){
					aliasesList.add(jpushCode);
				}
			}
			if(aliasesList.size()>0){
				Map<String, Object> params=new HashMap<String, Object>();
				params.put("newCount",1);
				ipushInterface.sendAllClientsPushMessage(aliasesList,"有新朋友啦!", params);
//				if(pushResult!=null&&pushResult.isResultOK()){
//					return true;
//				}
			}
//			throw new ServiceException(ErrorCode.INSERT_DATA_ERROR);
		}
		return true;
	}
	
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public boolean sendJpushMessage(String jpushCode) {
		if((!StringUtils.isEmpty(jpushCode)) &&jpushCode.length()>10){
			List<String> aliasesList=new ArrayList<String>();
			aliasesList.add(jpushCode);
			Map<String, Object> params=new HashMap<String, Object>();
			params.put("newCount",1);
			ipushInterface.sendAllClientsPushMessage(aliasesList,"有新朋友啦!", params);
//			if(pushResult!=null&&pushResult.isResultOK()){
//				return true;
//			}
//			throw new ServiceException(ErrorCode.INSERT_DATA_ERROR);
		}
		return true;
	}
	
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public User register(UserLoginForm userLoginForm) {
		User user = new User();
		user.setIsDelete(DeleteEnum.NOMAIL);
		user.setStatus(UserStatusEnum.UN_DATA);
		user.setLoginId(userLoginForm.getLoginId());
		user.setQrCode(userLoginForm.getLoginId());
		try {
			long countByMobile = shareRecipientLogDao.countByMobile(userLoginForm.getLoginId());
			if (countByMobile == UserResourceEnum.SHARE.getCode()) {
				user.setResource(UserResourceEnum.SHARE);
			}
			user.setMacUid(userLoginForm.getMacUid());
			user.setRegisterIp(userLoginForm.getIp());
			user.setBrowser(userLoginForm.getBrowser());
			user.setPassword(PasswordHashUtil.createHash(userLoginForm.getPassword()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (userDao.insert(user) > 0) {
			user.setShareCode(ShareCodeUtil.toSerialCode(user.getId()));
			userDao.update(user);

			UserAccount userAccount = new UserAccount();
			userAccount.setIntegral(0l);
			userAccount.setStatus(UserStatusEnum.NOMAIL);
			userAccount.setUserId(user.getId());
			userAccount.setBalance(BigDecimal.ZERO);
			userAccount.setRecharge(BigDecimal.ZERO);
			if (userAccountDao.insert(userAccount) > 0) {
				if (userConsunmeService.insertRecord(PointCodeEnum.REGISTER.getCode(), user.getId(),
						PointCodeMessage.getMsg(PointCodeMessage.REGISTER, CommonPointRule.getPoint(PointCodeEnum.REGISTER.getCode()))) != null) {
					return user;
				}
			}
		}
		throw new ServiceException(ErrorCode.INSERT_DATA_ERROR);
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public boolean updateUser(User user, UserInfo userInfo) {
		if (!StringUtils.isEmpty(userInfo.getHeadImage())) {
			user.setHeadImage(userInfo.getHeadImage());
		}
		if (userInfo.getGender() != null) {
			user.setGender(userInfo.getGender());
		}
		if (!StringUtils.isEmpty(userInfo.getUserName())) {
			user.setUserName(userInfo.getUserName());
		}
		if (userDao.update(user) > 0) {
			if (userInfoDao.update(userInfo) > 0) {
				peersDao.updateLastUpdateTime(user.getId());
				return true;
			}
		}
		throw new ServiceException(ErrorCode.UPDATE_DATA_ERROR);
	}

}